package cn.openea.perpartner.payment.web.wallet.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.openea.perpartner.payment.web.wallet.mapper.DialogMapper;
import cn.openea.perpartner.payment.web.wallet.entity.Dialog;
import cn.openea.perpartner.payment.web.wallet.service.IDialogService;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.TimerTask;

/**
 * (WalletDialog)表服务实现类
 *
 * @author makejava
 * @since 2023-07-21 16:17:38
 */
@Service("walletDialogService")
public class IDialogServiceImpl extends ServiceImpl<DialogMapper, Dialog> implements IDialogService {

    @Override
    public TimerTask recordDialog(String userId) {
        return new TimerTask() {
            @Override
            public void run() {
                Dialog dialog = getById(userId);
                // 如果没有记录则新增，否则更新
                if (Objects.isNull(dialog)) {
                    dialog = Dialog.builder().userId(userId).isPopup("0").build();
                    save(dialog);
                } else {
                    // 如果今天已经弹过了，则不再弹
                    dialog.setIsPopup("0");
                    updateById(dialog);
                }
            }
        };
    }

    @Override
    public AjaxResult getDialog(String userId) {
        Dialog dialog = getById(userId);
        boolean isPopup = !Objects.isNull(dialog) && StringUtils.equals(dialog.getIsPopup(), "0");
        return AjaxResult.success().put("isPopup", isPopup);
    }

    @Override
    public boolean updateDialogStatus(String userId) {
        return updateById(Dialog.builder().userId(userId).isPopup("1").build());
    }

}

